
//API key for http://openlayers.org. Please get your own at
//http://bingmapsportal.com/ and use that instead.
var apiKey = "AqTGBsziZHIJYYxgivLBf0hVdrAk9mWO5cQcb8Yux8sW5M8c8opEC2lZqKR1ZZXf";

//initialize map when page ready
var map;
var gg = new OpenLayers.Projection("EPSG:4326");
var sm = new OpenLayers.Projection("EPSG:900913");
OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';

OpenLayers.Util.onImageLoadError = function(){this.src='../images/blank.gif';}
OpenLayers.Tile.Image.useBlankTile=false;

function makeBikeVectorLayer(map)
{
    var vectors = new OpenLayers.Layer.Vector("vector", {isBaseLayer: true});
    map.addLayers([vectors]);

    var feature = new OpenLayers.Feature.Vector(
            OpenLayers.Geometry.fromWKT(
                "POLYGON((28.828125 0.3515625, 132.1875 -13.0078125, -1.40625 -59.4140625, 28.828125 0.3515625))"
            )
        );
        vectors.addFeatures([feature]);

    var feature2 = new OpenLayers.Feature.Vector(
        OpenLayers.Geometry.fromWKT(
            "POLYGON((-120.828125 -50.3515625, -80.1875 -80.0078125, -40.40625 -20.4140625, -120.828125 -50.3515625))"
        )
    );
    vectors.addFeatures([feature2]);

    var report = function(e) {
        OpenLayers.Console.log(e.type, e.feature.id);
    };

    var highlightCtrl = new OpenLayers.Control.SelectFeature(vectors, {
        hover: true,
        highlightOnly: true,
        renderIntent: "temporary",
        eventListeners: {
            beforefeaturehighlighted: report,
            featurehighlighted:       report,
            featureunhighlighted:     report
        }
    });

    var selectCtrl = new OpenLayers.Control.SelectFeature(vectors,
        {clickout: true}
    );

    map.addControl(highlightCtrl);
    map.addControl(selectCtrl);

    highlightCtrl.activate();
    selectCtrl.activate();
}


var init = function (onSelectFeatureFunction) {

 var vector = new OpenLayers.Layer.Vector("Vector Layer", {});

 var sprintersLayer = new OpenLayers.Layer.Vector("Sprinters", {
     styleMap: new OpenLayers.StyleMap({
         externalGraphic: "../images/mobile-loc.png",
         graphicOpacity: 1.0,
         graphicWith: 16,
         graphicHeight: 26,
         graphicYOffset: -26
     })
 });

 var sprinters = getFeatures();
 sprintersLayer.addFeatures(sprinters);

 var selectControl = new OpenLayers.Control.SelectFeature(sprintersLayer, {
     autoActivate:true,
     onSelect: onSelectFeatureFunction});

 var geolocate = new OpenLayers.Control.Geolocate({
     id: 'locate-control',
     geolocationOptions: {
         enableHighAccuracy: false,
         maximumAge: 0,
         timeout: 7000
     }
 });
 // create map
 map = new OpenLayers.Map({
     div: "map",
     theme: null,
     projection: sm,
     units: "m",
     numZoomLevels: 18,
     maxResolution: 156543.0339,
     maxExtent: new OpenLayers.Bounds(
         -20037508.34, -20037508.34, 20037508.34, 20037508.34
     ),
     controls: [
         new OpenLayers.Control.Attribution(),
         new OpenLayers.Control.TouchNavigation({
             dragPanOptions: {
                 interval: 100,
                 enableKinetic: true
             }
         }),
         geolocate,
         selectControl
     ],
     layers: [
         new OpenLayers.Layer.OSM("OpenStreetMap", null, {
             transitionEffect: 'resize'
         }),
         new OpenLayers.Layer.Bing({
             key: apiKey,
             type: "Road",
             // custom metadata parameter to request the new map style - only useful
             // before May 1st, 2011
             metadataParams: {
                 mapVersion: "v1"
             },
             name: "Bing Road",
             transitionEffect: 'resize'
         }),
         new OpenLayers.Layer.Bing({
             key: apiKey,
             type: "Aerial",
             name: "Bing Aerial",
             transitionEffect: 'resize'
         }),
         new OpenLayers.Layer.Bing({
             key: apiKey,
             type: "AerialWithLabels",
             name: "Bing Aerial + Labels",
             transitionEffect: 'resize'
         }),
         vector,
         sprintersLayer
     ],
     center: new OpenLayers.LonLat(0, 0),
     zoom: 1
 });

// makeBikeVectorLayer(map);

 var style = {
     fillOpacity: 0.1,
     fillColor: '#000',
     strokeColor: '#f00',
     strokeOpacity: 0.6
 };

 geolocate.events.register("locationupdated", this, function(e) {
     vector.removeAllFeatures();
     vector.addFeatures([
         new OpenLayers.Feature.Vector(
             e.point,
             {},
             {
                 graphicName: 'cross',
                 strokeColor: '#f00',
                 strokeWidth: 2,
                 fillOpacity: 0,
                 pointRadius: 10
             }
         ),
         new OpenLayers.Feature.Vector(
             OpenLayers.Geometry.Polygon.createRegularPolygon(
                 new OpenLayers.Geometry.Point(e.point.x, e.point.y),
                 e.position.coords.accuracy / 2,
                 50,
                 0
             ),
             {},
             style
         )
     ]);
     map.zoomToExtent(vector.getDataExtent());
 });

 function getFeatures() {
     var features = {
         "type": "FeatureCollection",
         "features": [
             { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-7909900, 5215100]},
                 "properties": {"name": "Hogan", "mode":"Bike lane", "street_id":"121657"}},
             { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-12362007.067301,5729082.2365672]},
                 "properties": {"name": "Lambert", "mode":"Low traffic through street", "street_id":"3"}},
         ]
     };

     var reader = new OpenLayers.Format.GeoJSON();

     return reader.read(features);
 }

};
